<?php
require_once "../class/fenajax.class.php";
include "../dyn/connexion.php";

$f = new fenAjax("Formulaire atelier",0);

if(isset($_POST['mode']) and isset($_POST['nom']) and isset($_POST['date_debut']) and isset($_POST['date_fin']) and isset($_POST['id_atelier']) and isset($_POST['theme'])) {
	
	$mode = $_POST['mode'];
	$nom = $_POST['nom'];
	$date_debut = $_POST['date_debut'];
	$date_fin = $_POST['date_fin'];
	$id_atelier = $_POST['id_atelier'];
	$theme = $_POST['theme'];
	
	if( !in_array($mode,array(1,2,3)))
		$f->ajaxErreur("Fonction non supportée","Ce mode n'est pas supporté pour ce module");
		
	if($mode == 1){			$header = "Créer l'atelier"; 		$nomAction = "Créer";		$disable = ""; }
	else if($mode == 2){	$header = "Modifier l'atelier"; 	$nomAction = "Modifier"; 	$disable = ""; }
	else{					$header = "Supprimer l'atelier"; 	$nomAction = "Supprimer"; 	$disable = ""; }
	
	$f->retourAjax["boutton"] = $nomAction;
	$f->retourAjax["header"] = $header;
	
	$form = "<div id=\"feedback\"></div><br />";
	
	if ($mode == 1) {
	
	
		$form .= "<script type=\"text/javascript\">
				datePicker();
			</script>
		";
		
		$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionAtelier($(this));\">";
	
		$form .= "<br /><label class=\"formInfos\" for=\"nom\">Nom :</label>
					<input class=\"ui-corner-all\" id=\"nom\" type=\"text\" value=\"".s($nom)."\" name=\"nom\" $disable/><br /><br />

					<label class=\"formInfos\" for=\"theme\">Thème :</label>
					<textarea class=\"theme ui-corner-all\" name=\"theme\"></textarea><br /><br />
					
					<label class=\"formInfos\" for=\"date_debut\">Date de début :</label>
					<input class=\"ui-corner-all\" type=\"text\" id=\"date_debut\" name=\"date_debut\"/><br /><br />
					
					<label class=\"formInfos\" for=\"date_fin\">Date de fin :</label>
					<input class=\"ui-corner-all\" type=\"text\" id=\"date_fin\" name=\"date_fin\"/>
					
					<input type=\"hidden\" name=\"id_atelier\" value=\"".$id_atelier."\" />

		";
	}
	
	if ($mode == 3) {
		
		$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionAtelier($(this));\">";
			
		$res = mysql_query("SELECT * FROM Atelier ORDER BY nom;");

		if (mysql_num_rows($res) == 0) {
			
			$form .= BMInfo("Il n'y a aucun atelier à supprimer");
			
			$form .= "<input type=\"hidden\" name=\"id_atelier\" value=\"".$id_atelier."\" />";
		}
		else {
			
			$form .= BMInfo("Note: La supression d'un atelier va entrainer la désinscription de ses participants");

			$prems = true;
			
			$form .= "<br /><label class=\"formInfos\" for=\"id_atelier\">Nom :</label>
						<select id=\"id_atelier\" name=\"id_atelier\" $disable>";
			
			while($row = mysql_fetch_array($res)) {
				$form .= "<option value=\"".$row['id']."\" ".($prems ? "selected=\"selected\"":"").">".$row['nom']."</option>";
				$prems = false;
			}
			
			$form .= "</select>";
		
		}

		$form .= "<input type=\"hidden\" name=\"nom\" value=\"".$nom."\" />
			<input type=\"hidden\" name=\"date_debut\" value=\"".$date_debut."\" />
			<input type=\"hidden\" name=\"date_fin\" value=\"".$date_fin."\" />
			<input type=\"hidden\" name=\"theme\" value=\"".$theme."\" />
		";
	}
	
	if ($mode == 2) {
		
		$form .= "<form action=\"#\" method=\"post\" onSubmit=\"return actionAtelierModif($(this));\">";
		
		$res = mysql_query("SELECT * FROM Atelier WHERE  id = '".$id_atelier."' ;");
		
		while($row = mysql_fetch_array($res)) {
			
			$form .= "<script type=\"text/javascript\">
					datePicker();
				</script>
			";
			
			$form .= "<br /><label class=\"formInfos\" for=\"nom\">Nom :</label>
					<input class=\"ui-corner-all\" id=\"nom\" type=\"text\" value=\"".$row['nom']."\" name=\"nom\" $disable/><br /><br />

					<label class=\"formInfos\" for=\"theme\">Thème :</label>
					<textarea class=\"theme ui-corner-all\" name=\"theme\">".$row['theme']."</textarea><br /><br />

					<label class=\"formInfos\" for=\"date_debut\">Date de début :</label>
					<input class=\"ui-corner-all\" type=\"text\" value=\"".$row['dateDebut']."\" id=\"date_debut\" name=\"date_debut\"/><br /><br />
					
					<label class=\"formInfos\" for=\"date_fin\">Date de fin :</label>
					<input class=\"ui-corner-all\" type=\"text\" value=\"".$row['dateFin']."\" id=\"date_fin\" name=\"date_fin\"/>
					
					<input type=\"hidden\" name=\"id_atelier\" value=\"".$id_atelier."\" />
			";
		}
	}
	
	
	$form .= "<input type=\"hidden\" name=\"mode\" value=\"".$mode."\" />
	</form>";
	
	$f->retourAjax["erreur"] = false;
	$f->retourAjax["msg"] = $form;
}

$f->endAjax();
?>
	